package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jt.pojo.Cart;
import com.jt.pojo.Order;
import com.jt.pojo.OrderComment;
import com.jt.pojo.OrderItem;
import com.jt.pojo.User;
import com.jt.service.DubboCartService;
import com.jt.service.DubboOrderService;
import com.jt.service.OrderFileService;
import com.jt.utils.ThreadLocalUtil;
import com.jt.vo.OrderPageVO;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author dc
 * @Classname OrderController
 * @Date 2020/2/29 15:02
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Reference(check = false,loadbalance = "roundrobin")
    private DubboCartService cartService;

    @Reference(check = false,loadbalance = "roundrobin")
    private DubboOrderService orderService;

    @Autowired
    private OrderFileService orderFileService;

    /**
     * 跳转订单确认页面
     * 根据userId查询购物车数据信息
     * @param model
     * @return
     */
    @RequestMapping("/create")
    public String create(Model model){
        Long userId = ThreadLocalUtil.getUser().getId();
        List<Cart> cartList = cartService.findCartListByUserId(userId);
        model.addAttribute("carts",cartList);
        return "order-cart";
    }

    /**
     * url:/order/submit
     * @param order form表单
     * @return SysResult对象
     */
    @RequestMapping("/submit")
    @ResponseBody
    public SysResult saveOrder(Order order){
        Long userId = ThreadLocalUtil.getUser().getId();
        order.setUserId(userId);
        Order newOrder = orderService.saveOrder(order);
        cartService.deleteCartAfterPay(order);
        if(StringUtils.isEmpty(newOrder)){
            return SysResult.fail();
        }
        return SysResult.success(newOrder);
    }

    /**
     * 提交订单成功
     * (根据orderId订单信息)
     * url:http://www.jt.com/order/success.html?id=91582968750010
     * 返回值:成功页面
     * 页面取值${order.orderId}
     */
    @RequestMapping("/success")
    public String FindOrderById(String orderId,String price,Model model){
        Order order = new Order();
        order.setPayment(price);
        order.setOrderId(orderId);
        Date date=new Date();
        Calendar calendar  =   Calendar.getInstance();
        //需要将date数据转移到Calender对象中操作
        calendar.setTime(date);
        //把日期往后增加n天.正数往后推,负数往前移动
        calendar.add(Calendar.DATE, 5);
        //这个时间就是日期往后推一天的结果
        date=calendar.getTime();
        model.addAttribute("order",order);
        model.addAttribute("date",date);
        return "success";
    }

    /**
     * 点击我的订单，根据用户id查询用户下所有订单，加载页面时加载数据
     * @param model mode模型
     * @return 订单数据
     */
    @RequestMapping("/myOrder")
    public String doMyOrder(Model model){
        return "my-orders";
    }

    @RequestMapping("/myOrder/orderList")
    @ResponseBody
    public SysResult orderList(String keyword,
                               @RequestParam(defaultValue = "1",required =false) Integer page,
                               @RequestParam(defaultValue = "5",required = false) Integer rows){
        Long userId = ThreadLocalUtil.getUser().getId();
        IPage<Order> iPage = orderService.findOrderByUserId(userId,keyword,page,rows);
        OrderPageVO pageVO = new OrderPageVO();
        pageVO.setData(iPage.getRecords())
                .setCurrentPage(page)
                .setRecordCount(iPage.getTotal())
                .setKeyword(keyword)
                .setLimit(rows);
        return SysResult.success(pageVO);
    }

    @RequestMapping("/comment/{orderId}/{itemId}")
    public String orderComment(@PathVariable String orderId,@PathVariable String itemId, Model model){
        OrderItem item=orderService.findItemByItemId(itemId,orderId);
        OrderComment comment = orderService.findOrderComment(itemId,orderId);
        model.addAttribute("row",comment);
        model.addAttribute("orderItem",item);
        return "my-order-comment";
    }

    @RequestMapping("/comment/picUpload")
    @ResponseBody
    public SysResult orderCommentPic(MultipartFile file){
        SysResult result=orderFileService.uploadFile(file);
        return result;
    }

    @RequestMapping("/comment/save")
    @ResponseBody
    public SysResult doSaveOrderComment(OrderComment orderComment){
        User user = ThreadLocalUtil.getUser();
        orderComment.setUserId(user.getId());
        orderService.saveOrderComment(orderComment);
        return SysResult.success();
    }

}
